package visitor;

import hypeerweb.Node;

import java.util.ArrayList;

public class TrackSendPathVisitor extends SendVisitor {
    public TrackSendPathVisitor() {
        super();
    }

    @SuppressWarnings("unchecked")
    @Override
    /**
     * Finishes constructing the path to this node and stores the path in the set of paths
     * that reached this node as target node in a send operation.
     * 
     * @param node the node being visited
     * @param parameters contains the target id for the send operation (identified by the key "TARGET_KEY")
     *        and contains the path from the start node to this node for the send operation.
     * 
     * @pre super.PRE_CONDITION AND parameters.contains(PATH_KEY) AND parameters.get(PATH_KEY) &ne null AND
     *      parameters.get(PATH_KEY) instanceof ArrayList<Integer>
     */
    protected void targetOperation(Node node, Parameters parameters) {
        ArrayList<Integer> path = (ArrayList<Integer>) parameters.get(PATH_KEY);
        path.add((int) node.getWebId().getValue());

        Contents contents = node.getContents();
        ArrayList<ArrayList<Integer>> paths = null;
        if (contents.containsKey(PATHS_KEY)) {
            paths = (ArrayList<ArrayList<Integer>>) contents.get(PATHS_KEY);
        } else {
            paths = new ArrayList<ArrayList<Integer>>();
            contents.set(PATHS_KEY, paths);
        }

        paths.add(path);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void intermediateOperation(Node node, Parameters parameters) {
        ArrayList<Integer> path = (ArrayList<Integer>) parameters.get(TrackSendPathVisitor.PATH_KEY);
        path.add((int) node.getWebId().getValue());
    }

    public static final String PATHS_KEY = "paths";
    public static final String PATH_KEY = "path";
}
